package version

import (
	"encoding/json"

	"github.com/spf13/cobra"
	"github.com/spf13/viper"
	yaml "gopkg.in/yaml.v2"

	"github.com/tendermint/tendermint/libs/cli"
)

const flagLong = "long"

func init() {
	Cmd.Flags().Bool(flagLong, false, "Print long version information")
}

// Cmd prints out the application's version information passed via build flags.
var Cmd = &cobra.Command{
	Use:   "version",
	Short: "Print the app version",
	RunE:  runVersionCmd,
}

func runVersionCmd(cmd *cobra.Command, args []string) error {
	verInfo := NewInfo()

	if !viper.GetBool(flagLong) {
		cmd.Println(verInfo.Version)
		return nil
	}

	var bz []byte
	var err error

	switch viper.GetString(cli.OutputFlag) {
	case "json":
		bz, err = json.Marshal(verInfo)
	default:
		bz, err = yaml.Marshal(&verInfo)
	}

	if err != nil {
		return err
	}

	cmd.Println(string(bz))
	return nil
}
